package com.app.service.impl;

import com.app.mapper.AreasMapper;
import com.app.mapper.CitiesMapper;
import com.app.mapper.ProvincesMapper;
import com.app.mapper.ReturnHomeMapper;
import com.app.model.AreasModel;
import com.app.model.CitiesModel;
import com.app.model.ProvincesModel;
import com.app.model.ReturnHomeModel;
import com.app.service.ReturnHomeService;
import com.app.util.DateMaskingUtils;
import com.app.util.StringTool;
import com.app.vo.ReturnHomeVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ReturnHomeServiceImpl extends ServiceImpl<ReturnHomeMapper, ReturnHomeModel> implements ReturnHomeService {
    @Autowired
    private ReturnHomeMapper returnHomeMapper;

    @Autowired
    private ProvincesMapper provincesMapper;

    @Autowired
    private CitiesMapper citiesMapper;

    @Autowired
    private AreasMapper areasMapper;

    @Override
    public ReturnHomeVo getReturnHomePageList(Integer page, Integer limit, String sex, String age, String education, String domicile, String workIndustry) {
        Page<ReturnHomeModel> page1 = new Page<>(page, limit);
        QueryWrapper<ReturnHomeModel> qw = new QueryWrapper<>();

        if (StringTool.isNotEmpty(sex)) {
            qw.like("sex", sex);
        }
        if (StringTool.isNotEmpty(age)) {
            if (age.equals("①16-20岁")){
                qw.gt("age",16);
                qw.le("age",20);
            }
            if (age.equals("②21-30岁")){
                qw.ge("age",21);
                qw.le("age",30);
            }
            if (age.equals("③31-40岁")){
                qw.ge("age",31);
                qw.le("age",40);
            }
            if (age.equals("④41-50岁")){
                qw.ge("age",41);
                qw.le("age",50);
            }
            if (age.equals("⑤51-60岁")){
                qw.ge("age",41);
                qw.le("age",50);
            }
            if (age.equals("⑥60岁以上")){
                qw.ge("age",41);
                qw.le("age",50);
            }
        }
        if (StringTool.isNotEmpty(education)) {
            qw.like("education", education);
        }
        if (StringTool.isNotEmpty(domicile)) {
            qw.like("domicile", domicile);
        }
        if (StringTool.isNotEmpty(workIndustry)) {
            qw.like("work_industry", workIndustry);
        }
        Page<ReturnHomeModel> ReturnHomePage = returnHomeMapper.selectPage(page1, qw);

        List<ReturnHomeModel> returnHomeList = ReturnHomePage.getRecords();
        for (ReturnHomeModel returnHome : returnHomeList) {

            //对县区id转义
            String areaid = returnHome.getAreaid();
            QueryWrapper<AreasModel> areasQW =new QueryWrapper<>();
            areasQW.eq("areaid",areaid);
            AreasModel areas = areasMapper.selectOne(areasQW);
            returnHome.setAreaIdText(areas.getArea());
            //对市id转义
            String cityId = areas.getCityid();
            QueryWrapper<CitiesModel> citiesQW=new QueryWrapper<>();
            citiesQW.eq("cityid",cityId);
            CitiesModel cities = citiesMapper.selectOne(citiesQW);
            returnHome.setCityIdText(cities.getCity());
            //对省id转义
            String provinceId = cities.getProvinceid();
            QueryWrapper<ProvincesModel> provincesQW =new QueryWrapper<>();
            provincesQW.eq("provinceid",provinceId);
            ProvincesModel provinces = provincesMapper.selectOne(provincesQW);
            returnHome.setProvinceIdText(provinces.getProvince());



        }

        long count = ReturnHomePage.getTotal();
        ReturnHomeVo returnHomeVo = new ReturnHomeVo();
        returnHomeVo.setList(returnHomeList);
        returnHomeVo.setCount((int) count);
        return returnHomeVo;

    }

    @Override
    public List<ReturnHomeModel> getReturnHomeList(String sex, String age, String education, String domicile, String workIndustry) {
        QueryWrapper<ReturnHomeModel> qw = new QueryWrapper<>();
        if (StringTool.isNotEmpty(sex)) {
            qw.like("sex", sex);
        }
        if (StringTool.isNotEmpty(age)) {
            if (age.equals("①16-20岁")){
                qw.gt("age",16);
                qw.le("age",20);
            }
            if (age.equals("②21-30岁")){
                qw.ge("age",21);
                qw.le("age",30);
            }
            if (age.equals("③31-40岁")){
                qw.ge("age",31);
                qw.le("age",40);
            }
            if (age.equals("④41-50岁")){
                qw.ge("age",41);
                qw.le("age",50);
            }
            if (age.equals("⑤51-60岁")){
                qw.ge("age",41);
                qw.le("age",50);
            }
            if (age.equals("⑥60岁以上")){
                qw.ge("age",41);
                qw.le("age",50);
            }
        }
        if (StringTool.isNotEmpty(education)) {
            qw.like("education", education);
        }
        if (StringTool.isNotEmpty(domicile)) {
            qw.like("domicile", domicile);
        }
        if (StringTool.isNotEmpty(workIndustry)) {
            qw.like("work_industry", workIndustry);
        }
        List<ReturnHomeModel> returnHomeList = returnHomeMapper.selectList(qw);
        return returnHomeList;
    }
}
